local sk__huaiyi = fk.CreateSkill {

  name = "sk__huaiyi",

  tags = {  },

}



sk__huaiyi:addEffect("active", {
  name = "sk__huaiyi",
  anim_type = "control",
  card_num = 0,
  target_num = 1,
  prompt = "#sk__huaiyi",
  derived_piles = "sk__gongsunyuan_yi",
  can_use = function (self, player)
    return player:usedSkillTimes(sk__huaiyi.name, Player.HistoryPhase) == 0
  end,
  card_filter = Util.FalseFunc,
  target_filter =function (self, player, to_select, selected, selected_cards)
        return #selected == 0 and to_select ~= player and not to_select:isNude()
  end,
  on_use = function(self, room, effect)
    local player = effect.from
    local target = effect.tos[1]
    local card = room:askForCardChosen(player, target, "he", sk__huaiyi.name, "#sk__huaiyi-choose::"..target.id)
    player:addToPile("sk__gongsunyuan_yi", card, false, sk__huaiyi.name, player.id)
  end,
})

sk__huaiyi:addEffect(fk.DrawNCards, {
  mute = true,
  derived_piles = "sk__gongsunyuan_yi",
  can_trigger = function(self, event, target, player, data)
    if target == player and player:hasSkill(sk__huaiyi.name) then
    return #player:getPile("sk__gongsunyuan_yi") > 0
    end
  end,
  on_cost = Util.TrueFunc,
  on_use = function(self, event, target, player, data)
    local room = player.room
    player:broadcastSkillInvoke("sk__huaiyi")
    room:notifySkillInvoked(player, "sk__huaiyi", "drawcard")
    data.n = data.n + #player:getPile("sk__gongsunyuan_yi")
  end,
})
sk__huaiyi:addEffect(fk.EventPhaseStart, {
  mute = true,
  derived_piles = "sk__gongsunyuan_yi",
  can_trigger = function(self, event, target, player, data)
    if target == player and player:hasSkill(sk__huaiyi.name) then
    return player.phase == Player.Finish and #player:getPile("sk__gongsunyuan_yi") > player.hp
    end
  end,
  on_cost = Util.TrueFunc,
  on_use = function(self, event, target, player, data)
    local room = player.room
    player:broadcastSkillInvoke("sk__huaiyi")
    room:notifySkillInvoked(player, "sk__huaiyi", "drawcard")
    local otherPlayerIds = table.map(room:getOtherPlayers(player), Util.IdMapper)
    room:doIndicate(player.id, otherPlayerIds)
    for _, p in ipairs(room:getOtherPlayers(player)) do
      if not p.dead then
        room:damage{
          from = player,
          to = p,
          damage = 1,
          skillName = "sk__huaiyi",
        }
      end
    end
    if not player.dead and #player:getPile("sk__gongsunyuan_yi") > 0 then
      room:moveCardTo(player:getPile("sk__gongsunyuan_yi"), Card.PlayerHand, player, fk.ReasonJustMove,
        "sk__huaiyi", nil, false, player)
    end
  end,
})

return sk__huaiyi